package top.zywork.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.zywork.common.BeanUtils;
import top.zywork.dao.GoodsCategoryDAO;
import top.zywork.dos.GoodsCategoryDO;
import top.zywork.dto.GoodsCategoryDTO;
import top.zywork.dto.PagerDTO;
import top.zywork.service.AbstractBaseService;
import top.zywork.service.GoodsCategoryService;
import top.zywork.vo.GoodsCategoryVO;
import top.zywork.vo.ResponseStatusVO;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;

/**
 * GoodsCategoryServiceImpl服务接口实现类<br/>
 *
 * 创建于2019-07-04<br/>
 *
 * @author http://zywork.top 王振宇
 * @version 1.0
 */
@Service(value = "goodsCategoryService")
public class GoodsCategoryServiceImpl extends AbstractBaseService implements GoodsCategoryService {

    private GoodsCategoryDAO goodsCategoryDAO;

    @Override
    public PagerDTO listCategoryByFirstLevelCategory(Object queryObj) {
        PagerDTO pagerDTO = new PagerDTO();
        List<Object> dtoObjList = new ArrayList<>();
        List<Object> doObjList = goodsCategoryDAO.listCategoryByFirstLevelCategory(queryObj);
        if (doObjList != null && doObjList.size() > 0) {
            dtoObjList = BeanUtils.copyList(doObjList, GoodsCategoryDTO.class);
        }
        pagerDTO.setRows(dtoObjList);
        pagerDTO.setTotal((long) dtoObjList.size());
        return pagerDTO;
    }

    @Override
    public ResponseStatusVO getLevel3InfoByCategory(Long categoryId) {
        Object obj = goodsCategoryDAO.getById(categoryId);
        if (null == obj) {
            return ResponseStatusVO.error("id错误", null);
        }
        GoodsCategoryVO goodsCategoryVO = BeanUtils.copy(obj, GoodsCategoryVO.class);
        if (goodsCategoryVO.getCategoryLevel() != 3) {
            return ResponseStatusVO.error("id错误,该类目不是3级类目", null);
        }
        // 根据父级类目id获取顶级类目的id
        Object parentObj = goodsCategoryDAO.getById(goodsCategoryVO.getParentId());
        GoodsCategoryVO parentGoodsCategoryVO = BeanUtils.copy(parentObj, GoodsCategoryVO.class);
        goodsCategoryVO.setTopId(parentGoodsCategoryVO.getId());
        return ResponseStatusVO.ok("查询成功", goodsCategoryVO);
    }

    @Autowired
    public void setGoodsCategoryDAO(GoodsCategoryDAO goodsCategoryDAO) {
        super.setBaseDAO(goodsCategoryDAO);
        this.goodsCategoryDAO = goodsCategoryDAO;
    }

    @PostConstruct
    public void init() {
        super.init(GoodsCategoryDO.class, GoodsCategoryDTO.class);
    }
}
